import 'dart:convert';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:url_launcher/url_launcher_string.dart';

import '../widgets/photo_view_gallery_screen.dart';

///
///Author: YYF
///Date: 2022-04-01 10:15:55
///Description:
///

class Utils {
  /// 隐藏键盘
  static void hiddenKeyBorder([BuildContext? context]) =>
      FocusScope.of(context ?? Get.context!).requestFocus(FocusNode());

  /// 拨打电话、打开外部浏览器
  static Future<void> launchInBrowser(String url) async {
    if (url.contains('www.') && !url.contains('http')) {
      url = 'http://$url';
    }
    if (await canLaunchUrlString(url)) {
      await launchUrlString(url);
    } else {
      debugPrint('不能打开 $url');
    }
  }

  ///查看大图
  toViewBigPhoto({required context, required List images, required index}) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => PhotoViewGalleryScreen(
                  images: images,
                  heroTag: '0',
                  index: index,
                )));
  }

// md5 加密
  static String generateMd5(String data) {
    var content = const Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }

  static bool haveNewVersion(
      {required String newVersion, required String old}) {
    if (newVersion.isEmpty || old.isEmpty) {
      return false;
    }
    int newVersionInt, oldVersion;
    var newList = newVersion.split('.');
    var oldList = old.split('.');
    if (newList.isEmpty || oldList.isEmpty) {
      return false;
    }
    for (int i = 0; i < newList.length; i++) {
      newVersionInt = int.parse(newList[i]);
      oldVersion = int.parse(oldList[i]);
      if (newVersionInt > oldVersion) {
        return true;
      } else if (newVersionInt < oldVersion) {
        return false;
      }
    }
    return false;
  }

  ///时间格式 2020-02-22 13:47:16
  static getTimeToNowDate(String oldTime) {
    String nowTime = DateTime.now().toString().split('.')[0];

    int nowyear = int.parse(nowTime.split(" ")[0].split('-')[0]);
    int nowmonth = int.parse(nowTime.split(" ")[0].split('-')[1]);
    int nowday = int.parse(nowTime.split(" ")[0].split('-')[2]);
    int nowhour = int.parse(nowTime.split(" ")[1].split(':')[0]);
    int nowmin = int.parse(nowTime.split(" ")[1].split(':')[1]);

    int oldyear = int.parse(oldTime.split(" ")[0].split('-')[0]);
    int oldmonth = int.parse(oldTime.split(" ")[0].split('-')[1]);
    int oldday = int.parse(oldTime.split(" ")[0].split('-')[2]);
    int oldhour = int.parse(oldTime.split(" ")[1].split(':')[0]);
    int oldmin = int.parse(oldTime.split(" ")[1].split(':')[1]);

    var now = DateTime(nowyear, nowmonth, nowday, nowhour, nowmin);
    var old = DateTime(oldyear, oldmonth, oldday, oldhour, oldmin);
    var difference = now.difference(old);

    if (difference.inDays > 30) {
      return '${difference.inDays ~/ 30}月前';
    } else if (difference.inDays > 1) {
      return '${difference.inDays}天前';
    } else if (difference.inDays == 1) {
      return '昨天'.toString();
    } else if (difference.inHours >= 1 && difference.inHours < 24) {
      return '${difference.inHours}小时前';
    } else if (difference.inMinutes > 5 && difference.inMinutes < 60) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inMinutes <= 5) {
      return '刚刚';
    }
  }

  ///取出所有空格
  String trimAllSpace(String str) {
    return str.replaceAll(RegExp(r"\s+\b|\b\s"), "");
  }

  ///判断当前应用处于debug模式
  static bool get isInDebugMode {
    bool inDebugMode = false;
    assert(inDebugMode = true); //如果debug模式下会触发赋值
    return inDebugMode;
  }

  ///获取某一个月的最后一天。
  static DateTime getEndMothFor([DateTime? startDate]) {
    startDate = startDate ?? DateTime.now();
    DateTime nextTime = DateTime(startDate.year, startDate.month + 1, 1);
    DateTime oldTime =
        DateTime(nextTime.year, nextTime.month, nextTime.day - 1);

    return oldTime;
  }

  static String getEPositiveUnitStr(ePositive) {
    double _ePositive = 0;
    String ePositiveUnit = 'kWh';
    if (ePositive is String) {
      _ePositive = double.parse(ePositive);
    } else if (ePositive is num) {
      _ePositive = ePositive.toDouble();
    }

    if (_ePositive > 10000) {
      _ePositive = _ePositive / 10000;
      ePositiveUnit = '万kWh';
    }
    if (_ePositive > 10000) {
      _ePositive = _ePositive / 10000;
      ePositiveUnit = '亿kWh';
    }
    return _ePositive.toStringAsFixed(2) + ePositiveUnit;
  }

  static String getMoneyUnitStr(money) {
    double _money = 0;
    String moneyUnit = '元';
    if (money is String) {
      _money = double.parse(money);
    } else if (money is num) {
      _money = money.toDouble();
    }

    if (_money > 10000) {
      _money = _money / 10000;
      moneyUnit = '万元';
    }
    if (_money > 10000) {
      _money = _money / 10000;
      moneyUnit = '亿元';
    }
    return _money.toStringAsFixed(2) + moneyUnit;
  }

  ///只返回单位
  static List getEUnitStr(ePositive) {
    double _ePositive = 0;
    String ePositiveUnit = 'kWh';
    if (ePositive is String) {
      _ePositive = double.parse(ePositive);
    } else if (ePositive is num) {
      _ePositive = ePositive.toDouble();
    }

    if (_ePositive > 10000) {
      _ePositive = _ePositive / 10000;
      ePositiveUnit = '万kWh';
    }
    if (_ePositive > 10000) {
      _ePositive = _ePositive / 10000;
      ePositiveUnit = '亿kWh';
    }
    return [_ePositive, ePositiveUnit];
  }

  ///只返回单位
  static List getMUnitStr(money) {
    double _money = 0;
    String moneyUnit = '元';
    if (money is String) {
      _money = double.parse(money);
    } else if (money is num) {
      _money = money.toDouble();
    }

    if (_money > 10000) {
      _money = _money / 10000;
      moneyUnit = '万元';
    }
    if (_money > 10000) {
      _money = _money / 10000;
      moneyUnit = '亿元';
    }
    return [_money, moneyUnit];
  }
}
